package ubo.master1.jeupoker.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import ubo.master1.jeupoker.ia.LesActions;


public class ControlPrincipal extends JPanel implements ActionListener {

    /** Monitor attend une action utilisateur. */
    private final Object monitor = new Object();
    /** le bouton Check (passer). */
    private final JButton checkButton;
    /** le bouton call suit la mise. */
    private final JButton callButton;
    /** le bouton Bet (miser). */
    private final JButton betButton;
    /** le bouton Raise (Agmenter la mise). */
    private final JButton raiseButton;
    /** le bouton Fol (se coucher). */
    private final JButton foldButton;
    /** le bouton Continue. */
    private final JButton continueButton;

    private LesActions action;
  
    /**
     * Constructeur.
     * 
     * @param main
     *            fenetre principale.
     */
    public ControlPrincipal() {
        this.setOpaque(false);
        setLayout(new FlowLayout());
        continueButton = createActionButton(LesActions.CONTINUE);
        checkButton = createActionButton(LesActions.CHECK);
        callButton = createActionButton(LesActions.CALL);
        betButton = createActionButton(LesActions.BET);
        raiseButton = createActionButton(LesActions.RAISE);
        foldButton = createActionButton(LesActions.FOLD);
    }
 /**
     * Attendre que l'utilisateur appuis sur continue.
     */
    public void waitForUserInput() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                removeAll();
                add(continueButton);
                validate();
                repaint();}
        });
        Set<LesActions> allowedActions = new HashSet<LesActions>();
        allowedActions.add(LesActions.CONTINUE);
        getUserInput(allowedActions);
    }
    /**
     *Attendre que l'utilisateur choisit une acgtion et retourner sa séléction.
     * 
     * @param allowedActions
     *            Les action autorisé.
     * 
     * @return L'action séléctionné.
     */
    public LesActions getUserInput(final Set<LesActions> allowedActions) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                // Affichage des boutons des actions autorié .
                removeAll();
                if (allowedActions.contains(LesActions.CONTINUE)) {
                    add(continueButton);
                } else {
                    if (allowedActions.contains(LesActions.CHECK)) {
                        add(checkButton);
                    }
                    if (allowedActions.contains(LesActions.CALL)) {
                        add(callButton);
                    }
                    if (allowedActions.contains(LesActions.BET)) {
                        add(betButton);
                    }
                    if (allowedActions.contains(LesActions.RAISE)) {
                        add(raiseButton);
                    }
                    if (allowedActions.contains(LesActions.FOLD)) {
                        add(foldButton);
                    }
                }
                validate();
                repaint();
            }
        });
        synchronized (monitor) {
            try {
                monitor.wait();
            } catch (InterruptedException e) {
                System.out.println("Error "+e.getMessage());
            }
        }
        return action;
    }

    /*
     * (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == continueButton) {
            action = LesActions.CONTINUE;
        } else if (source == checkButton) {
            action = LesActions.CHECK;
        } else if (source == callButton) {
            action = LesActions.CALL;
        } else if (source == betButton) {
            action = LesActions.BET;
        } else if (source == raiseButton) {
            action = LesActions.RAISE;
        } else {
            action = LesActions.FOLD;
        }
        synchronized (monitor) {
            monitor.notifyAll();
        }
    }
    /**
     * Créer une bouton selon les action disponible.
     * 
     * @param action
     *            L'action.
     * 
     * @return Le bouton.
     */
    private JButton createActionButton(LesActions action) {
        String label = action.getName();
        JButton button = new JButton(label);
        button.setMnemonic(label.charAt(0));
        button.setSize(100, 30);
        button.addActionListener(this);
        return button;
    }
}
